package com.work.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.bean.UserBean;
import com.work.mapper.UserMapper;
import com.work.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class IUserServiceImpl extends ServiceImpl<UserMapper, UserBean> implements IUserService {


    @Override
    public UserBean login(String userName, String userPwd) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_name", userName);
        wrapper.eq("user_pwd", userPwd);
        UserBean userBean = this.getOne(wrapper);
        return userBean;
    }

    @Override
    public IPage<UserBean> findBuItem(int pageNo, int pageSize) {
        Page<UserBean> page = new Page<>(pageNo, pageSize);
        IPage<UserBean> iPage = this.page(page);
        return iPage;
    }

    @Override
    public void addUser(UserBean userBean) {
        List<UserBean> list = new ArrayList<>();
        for (UserBean u : list) {
            if (userBean.getUserName().equals(u.getUserName())) {
                return;
            }
        }
        baseMapper.insert(userBean);
    }

    @Override
    public void delUser(int userId) {
        this.baseMapper.deleteById(userId);
    }

    @Override
    public void updateUser(int userId, String userPwd, String userType) {
        UserBean userBean = baseMapper.selectById(userId);
        userBean.setUserPwd(userPwd);
        userBean.setUserType(userType);
        this.baseMapper.updateById(userBean);
    }

    @Override
    public IPage<UserBean> findType(int pageNo, int pageSize, String userType) {
        Page<UserBean> page = new Page<>(pageNo, pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if (userType != null && userType.equals("")) {
            wrapper.eq("user_type", userType);
        }
        IPage<UserBean> iPage = this.page(page, wrapper);
        return iPage;
    }


}
